//
//  YKOAuthViewController.m
//  youkan
//
//  Created by mac mini on 16/2/27.
//  Copyright © 2016年 Honsintech. All rights reserved.
//

#import "YKOAuthViewController.h"

#import "YKNewFeatureController.h"
#import "YKTabBarController.h"
#import "YKAccount.h"
#import "YKAccountTool.h"
#import "YKRootTool.h"

@interface YKOAuthViewController ()<UIWebViewDelegate>
/** <#注释#> */
@property (nonatomic ,weak)UIWebView *web;
@end

@implementation YKOAuthViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    登录网页--UIWebView
    self.view.backgroundColor =[UIColor blueColor];
    UIWebView *web=[[UIWebView alloc]initWithFrame:self.view.bounds];
    web.backgroundColor =[UIColor grayColor];
    web.delegate = self;
    [self.view addSubview:web];
    self.web = web ;
//https://api.weibo.com/oauth2/authorize?
    //client_id=1005853160&redirect_uri=https://hao.360.cn/
//https://hao.360.cn/?
    //code=55d86a6432e9b3671c20977af0ff4ada

    NSString *client_id=@"1005853160";
    NSString *redirect_uri=@"https://hao.360.cn/";
    NSString *urlBase =  @"https://api.weibo.com/oauth2/authorize";
    NSString *urlStr=[NSString stringWithFormat:@"%@?client_id=%@&redirect_uri=%@",urlBase,client_id,redirect_uri];
    NSURL *url=[NSURL URLWithString:urlStr];
    NSURLRequest *request =[NSURLRequest requestWithURL:url];
    [web loadRequest:request];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"自动注册" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(autoRegister) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [btn sizeToFit];
    [btn setBackgroundColor:[UIColor whiteColor]];
    btn.top = kStatusBarHeight;
    
    [web addSubview:btn];
//    let js = "document.getElementById('userId').value = '13667102767';"
//    + "document.getElementById('passwd').value = '136_wgf_wb';"
}
-(void)webViewDidStartLoad:(UIWebView *)webView{
    [MBProgressHUD showMessage:@"加载中..."];

}
-(void)webViewDidFinishLoad:(UIWebView *)webView{
    [MBProgressHUD hideHUD];
}
//拦截web的请求,加载请求时调用这个方法，询问是否请求
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{

    NSString *urlStr = request.URL.absoluteString;
//    YKLog(@"urlStr===%@",urlStr);
    NSRange range =[urlStr rangeOfString:@"code="];
    if (range.length) {
        NSString *code =[urlStr substringFromIndex:range.location+range.length];
        [self accessTokenWithCode:code];
        return NO;
    }
    return YES;
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
        [MBProgressHUD hideHUD];

}

-(void)accessTokenWithCode:(NSString*)code{

[YKAccountTool accountWithCode:code success:^{
//    请求成功后选择跟控制器
    [YKRootTool chooseRootViewController:YKKeyWindow];
} failure:^(NSError *error) {
    YKLog(@"root==%@",error);
}];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)autoRegister{
    NSString *js = @"document.getElementById('userId').value = '13667102767';"@"document.getElementById('passwd').value = '136_wgf_wbsb';";
    [self.web stringByEvaluatingJavaScriptFromString:js];
}

@end
